
//
// Copyright 2009 Arne Halvorsen (arne.halvorsen@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

// Project page: http://code.google.com/p/mf4j/

//
// 091116 - aha42 - Starts in line change tracking.
// 091119 - aha42 - Renamed from PrintFlickrVisitor.
// 091121 - aha42 - Added project page link (google code) to header comment.
// 091128 - aha42 - Missing @Overide.
// 091217 - aha42 - print(FlickrObject) and print(FlickrObject[]). 
//

package org.aha.mf4j.visitor;

import org.aha.mf4j.response.FlickrObject;

/**
 * <p>
 *   {@link Visitor} for printing hierarchy to
 *   {@link System#out standard out}.
 * </p>
 * @author Arne Halvorsen (aha42)
 */
public final class PrintVisitor extends Visitor 
{
  /**
   * <p>
   *   Shared single instance.
   * </p>
   */
  public static final Visitor INSTANCE=new PrintVisitor();
  
  // Singleton pattern dictates private constructor.
  private PrintVisitor(){}

  @Override
  public boolean visit(int level, FlickrObject visited) 
  {
    StringBuilder sb=new StringBuilder();
    for (int i=0; i<level; i++) sb.append(' ');
    sb.append(visited);
    System.out.println(sb.toString());
    return true;
  }
  
  /**
   * <p>
   *   Prints hierarchy.
   * </p>
   * @param root Root of hierarchy to print.
   */
  public static void print(FlickrObject root)
  {
    if (root==null)
    {
      throw new NullPointerException("root");
    }
    
    root.visit(INSTANCE);
  }
  
  /**
   * <p>
   *   Prints hierarchies.
   * </p>
   * @param forest Roots of hierarchies to print.
   */
  public static void print(FlickrObject[] forest)
  {
    if (forest==null)
    {
      throw new NullPointerException(); 
    }
    
    FlickrObject.visit(forest, INSTANCE);
  }
  
}
